﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BUS.DAL
{
    public class ChiTietDatHangDAL
    {

        internal static void InsertAll(DatHangBUS datHangBUS)
        {
            ShopDataContext db = DataAccess.Database;
            List<ChiTietDatHang> chiTiets = new List<ChiTietDatHang>();

            foreach (ChiTietDatHangBUS ct in datHangBUS.ChiTietDatHangs)
            {
                ChiTietDatHang newct = new ChiTietDatHang()
                {
                    IDDatHang = datHangBUS.ID,
                    IDSanPham = ct.IDSanPham,
                    SoLuong = ct.SoLuong,
                    ThanhTien = ct.ThanhTien,
                    Duyet = false
                };
                chiTiets.Add(newct);
            }

            db.ChiTietDatHangs.InsertAllOnSubmit(chiTiets);
            db.SubmitChanges();

        }

        internal static List<ChiTietDatHangBUS> GetByDatHang(int idDatHang)
        {
            ShopDataContext db = DataAccess.Database;

            var result = from p in db.ChiTietDatHangs
                         where p.IDDatHang == idDatHang
                         select new ChiTietDatHangBUS()
                         {
                             ID = p.ID,
                             SanPham = new SanPhamBUS()
                             {
                                 ID = p.SanPham.ID,
                                 MaSanPham = p.SanPham.MaSanPham,
                                 TenSanPham = p.SanPham.TenSanPham,

                                 LoaiSanPham = new LoaiSanPhamBUS()
                                 {
                                     ID = p.SanPham.LoaiSanPham.ID,
                                     TenLoai = p.SanPham.LoaiSanPham.TenLoai
                                 },

                                 Gia = (decimal)p.SanPham.Gia,
                                 Anh = p.SanPham.Anh,
                                 SoTon = (int)p.SanPham.SoTon
                             },
                             SoLuong = (int)p.SoLuong,
                             ThanhTien = (decimal)p.ThanhTien,
                             Duyet = (bool)p.Duyet
                         };
            return result.ToList<ChiTietDatHangBUS>();

        }

        internal static void Delete(ChiTietDatHangBUS chiTietDatHangBUS)
        {
            ShopDataContext db = DataAccess.Database;

            ChiTietDatHang result = db.ChiTietDatHangs.SingleOrDefault<ChiTietDatHang>(p => p.ID == chiTietDatHangBUS.ID);
            db.ChiTietDatHangs.DeleteOnSubmit(result);

            db.SubmitChanges();
        }

        internal static void UpdateDuyet(ChiTietDatHangBUS chiTietDatHangBUS)
        {
            ShopDataContext db = DataAccess.Database;

            ChiTietDatHang result = db.ChiTietDatHangs.SingleOrDefault<ChiTietDatHang>(p => p.ID == chiTietDatHangBUS.ID);

            result.Duyet = chiTietDatHangBUS.Duyet;

            db.SubmitChanges();
        }

        internal static List<ChiTietDatHangBUS> GetProductsByCatesAndDate(DateTime dfrom, DateTime dto)
        {
            ShopDataContext db = DataAccess.Database;

            var result = from p in db.SanPhams
                         orderby p.ID
                         select new ChiTietDatHangBUS()
                         {
                             SanPham = new SanPhamBUS()
                             {                                 
                                 MaSanPham = p.MaSanPham,
                                 TenSanPham = p.TenSanPham,

                                 LoaiSanPham = new LoaiSanPhamBUS()
                                 {
                                     ID = p.LoaiSanPham.ID,
                                     TenLoai = p.LoaiSanPham.TenLoai
                                 },
                                 Gia = (decimal)p.Gia,
                                 SoTon = (int)p.SoTon
                             },

                             SoLuong = p.ChiTietDatHangs.Where(o => o.Duyet == true && o.DatHang.ThoiGianDatHang >= dfrom && o.DatHang.ThoiGianDatHang <= dto).Sum(q => q.SoLuong) == null ? 0 : (int)p.ChiTietDatHangs.Where(o => o.Duyet == true && o.DatHang.ThoiGianDatHang >= dfrom && o.DatHang.ThoiGianDatHang <= dto).Sum(q => q.SoLuong)
                             //ThanhTien = (decimal)p.ThanhTien,                          
                         };
            return result.ToList<ChiTietDatHangBUS>();
        }
    }
}
